/**
 * Created by songj on 2015/12/11.
 */
define(['angular',
    'framework/msg/GillionMsgModule',
    'framework/dataGrid/DataGridModule',
    'framework/pagination/GillionPaginationModule',
    'framework/datasource/DataSourceModule',
    'framework/dropdown/GillionDropdownModule',
    'framework/date/DateModule',
    'angular-underscore'
], function (angular) {
    var module = angular.module('CompileController',
        ['GillionMsgModule', 'DataGridModule', 'GillionPaginationModule',
            'DataSourceModule', 'DateModule', 'GillionDropdownModule'])
        .controller('CompileController', ['$scope', 'GillionMsgService', '$dataSourceManager', '$timeout', 'Arrays', "$location",'Resource','GillionMsg','$compile','$http','$compile',
                function ($scope, GillionMsgService, $dataSourceManager, $timeout, Arrays, $location,Resource,GillionMsg,$compile) {
                    $scope.responseLog = GillionMsgService.getInputData();
                    $scope.logShow=true;
                    $scope.process=true;
                    $scope.search = {
                        responseLog:  $scope.responseLog
                    };
                    $scope.showLog = function(){
                        if($scope.logShow){
                            $scope.logShow=false;
                            $("#logDetail").css("height","0px");
                            $("#img").attr("class","icon-engine-upward-b");
                            window.resizeTo(500, 100);
                        }else{
                            $scope.logShow=true;
                            $("#logDetail").css("height","180px");
                            $("#img").attr("class","icon-engine-down-b");
                            window.resizeTo(500, 300);
                        }
                    };

                    $timeout(function () {
                        var logDetail = $scope.responseLog.log.success ? $scope.responseLog.log.data : $scope.responseLog.log.errorMessages[0],
                            lines = logDetail.split("\n"),
                            lineCount = lines.length,
                            $logDetail = angular.element("#logDetail");
                        $scope.logDetail = "";
                        var printCount = 0;
                        /*var print = function (lineBegin, lineEnd) {
                            return function () {
                                var printLines = lines.slice(lineBegin, lineEnd);
                                angular.forEach(printLines, function (line) {
                                    console.log(line);
                                    $scope.logDetail += line + "\n";
                                });
                                $logDetail.scrollTop( $logDetail[0].scrollHeight);
                            };
                        };
                        var i = 0;
                        while (printCount < lineCount) {
                            var printLine = Math.random() * 10;
                            var lineBegin = printCount;
                            var lineEnd = printCount + printLine;
                            printCount = printCount + printLine;
                            $timeout(print(lineBegin, lineEnd), 100 * (i++));
                        }*/
                        $scope.logDetail=logDetail;

                        $timeout(function () {
                            $scope.process = false;
                            if ($scope.responseLog.log.success) {
                                angular.element("#mes").html("<img src='/static/images/cg.png' style='vertical-align:middle;padding-right:10px;'>编译成功！");
                            } else {
                                angular.element("#mes").text("编译失败！");
                            }
                        }, 300*(i++));
                    },100);
                }
            ]
        );

    module.config(['$locationProvider', function($locationProvider) {
        $locationProvider.html5Mode(true);
    }]);
    return module;
});
